#include "av.h"
#include "server_http.h"

/*
MHD_Daemon* ListenPort(int port)
{
    struct MHD_Daemon* daemon = 
        MHD_start_daemon(MHD_USE_SELECT_INTERNALLY, 
        port, 
        NULL, NULL, 
        connectionHandler, 
        NULL, MHD_OPTION_END);
    if (daemon == NULL) {
        printf("cannot start server!\n");
        return NULL;
    }

    return daemon;
}
*/

void ListenPort(int port)
{
    static web_server our_web_server;
    our_web_server.set_listening_port(port);
    our_web_server.start_async();
    printf("Listening port %d\n", port);
}


int main(int argc, const char** argv)
{
    bool isHelp = false;
    int port = 10062;
    for (int i = 1; i < argc; i++)
    {
        if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0)
        {
            isHelp = true;
        }
        else if (strcmp(argv[i], "-p") == 0)
        {
            if (i >= argc - 1)
	    {
                printf("Error: param -p need a PORT\n");
                return -1;
	    }
            port = atoi(argv[++i]);
        }
        else if (strncmp(argv[i], "--port=", 7) == 0)
        {
            port = atoi(argv[i] + 7);
        }
    }
    if (isHelp)
    {
        printf("-h or --help for help\n-p PORT or --port=PORT for listen port\n");
        return 0;
    }
    srand(time(0));

    AntiVirus::GetInstance()->Init();
    //MHD_Daemon* daemon = ListenPort(port);
    ListenPort(port);

    while (true)
    {
        sleep(5);
    }

    //MHD_stop_daemon(daemon);
    return 0;
}
